home *** CD-ROM | disk | FTP | other *** search
/ Aminet 15 / Aminet 15 - Nov 1996.iso / Aminet / comm / mail / YamNet.lha / FSCode.lha / Rexx / EMS / ExtractFile.ems
Text File  |  1995-06-24  |  5KB  |  167 lines

  1. /*
  2. ** $VER: ExctractFile.ems 1.003 (30.03.95)
  3. **
  4. ** $HISTORY:
  5. **
  6. ** 30 Mar 1995 : 001.003 : Added checks for the presence of the ending line
  7. **                          by Flavio Stanchina
  8. ** 04 Dec 1993 : 001.002 : Modified TEMP_FILE handling and minor changes
  9. **                         by Flavio Stanchina 2:333/408.9fidonet.org
  10. ** 27 Nov 1993 : 001.001 : Added support for FScode by Gian Maria Calzolari
  11. **                         2:332/502.11, 2:332/508.19
  12. ** 29 Aug 1993 : 001.000 : Updated by S. Wijman 2:2802/123@fidonet
  13. **
  14. */
  15.  
  16. options results
  17. signal on error
  18. signal on syntax
  19.  
  20. parse arg area_name msg_num .
  21.  
  22. if datatype( msg_num, 'N' ) ~= 1 then call Usage
  23.  
  24. if( ~show( 'l', "ems_rexx.library" ) ) then
  25. do
  26.    if( ~addlib( "ems_rexx.library", 0, -30, 0 ) )then
  27.    do
  28.       say "Could not open ems_rexx.library"
  29.       exit 10
  30.    end
  31. end
  32.  
  33. cmd.0       = 3                          /* Decoder number                */
  34. cmd.1       = 'C:UUDecode'               /* Full path to command UUDecode */
  35. cmd.2       = 'C:UUDecode'               /* Full path to command UUDecode */
  36. cmd.3       = 'C:FSCode'                 /* Full path to command FSCode   */
  37.  
  38. cmd.1.bgn   = 'begin 777'                /* What should I search for?     */
  39. cmd.2.bgn   = 'begin 644'
  40. cmd.3.bgn   = '!start'
  41.  
  42. cmd.1.end   = 'end'                      /* What will I find at the end?  */
  43. cmd.2.end   = 'end'
  44. cmd.3.end   = '!end'
  45.  
  46. cmd.1.wnum  = 3                          /* Which word is the name?       */
  47. cmd.2.wnum  = 3
  48. cmd.3.wnum  = 2
  49.  
  50. delete_name = EMS_Var_Local( 'SCRIPT_DELETE_NAME' )
  51. temp_dir    = EMS_Var_Local( 'TEMP_DIR'           )
  52. temp_file   = 'T:' || area_name || '.' || msg_num || '.Ext' /* FS */
  53. cr          = '0a'x
  54.  
  55. /* Init of some variabiles */
  56. names.0 = 0
  57.  
  58. call EMS_Item_Alloc(     'msg', area_name, 'MSG', msg_num )
  59. call EMS_Item_Lines_Get( 'msg', 'TEXT', 'lines'           )
  60. call EMS_Item_Free(      'msg'                            )
  61.  
  62. old_dir = pragma( 'D', temp_dir )
  63.  
  64. do k = 1 to cmd.0
  65.     match = 0
  66.  
  67.     do forever
  68.         match = EMS_Search_In_Stem( 'lines', cmd.k.bgn || '#?', match )
  69.  
  70.         if match = 0 then leave
  71.  
  72.         if EMS_Search_In_Stem( 'lines', cmd.k.end || '#?', match ) = 0 then do
  73.           call EMS_Do_Request( 'Found "' || cmd.k.bgn || '",' || cr || 'but no "' || cmd.k.end || '"!', 'Continue' )
  74.           call QuitScript
  75.         end
  76.  
  77.         line = lines.match
  78.         name = word( line, cmd.k.wnum )
  79.         
  80.         parse var name name1 ':' name2
  81.         if name2 ~= '' then name = name2
  82.  
  83.         parse var name name1 '/' name2 .
  84.         if name2 ~= '' then name = name2
  85.  
  86.         num       = names.0 + 1
  87.         names.0   = num
  88.         names.num = name
  89.  
  90.         if exists( name ) then do
  91.             select = EMS_Do_Request( cmd.k 'header found' || cr || 'Original FileName:' name || cr || cr || 'A file with the same name exists in the' temp_dir 'directory.' || cr || 'Should I rename it to "' || name || '.EMS" ?', 'Yes|No' )
  92.  
  93.             if select = 'Yes' then do
  94.                 name      = name'.EMS'
  95.                 names.num = name
  96.               end
  97.               else do
  98.                 select = EMS_Do_Request( 'Should I delete the existing file ?', 'Yes|No' )
  99.  
  100.                 if select = 'No' then do
  101.                     call EMS_Do_Request( 'Extracting' cmd.k 'files has been stopped.' )
  102.                     call QuitScript
  103.                 end
  104.  
  105.                 address command delete_name name
  106.             end
  107.         end
  108.  
  109.         if open( 'out', temp_file, 'W' ) ~= 1 then do
  110.             call EMS_Do_Request( 'Cannot open "' || temp_file || '" for output.' )
  111.             call QuitScript
  112.         end
  113.  
  114.         call writeln( 'out', cmd.k.bgn name )
  115.  
  116.         do forever
  117.             match = match + 1
  118.             line  = lines.match
  119.             call writeln( 'out', line )
  120.  
  121.             if left( line, length( cmd.k.end ) ) = cmd.k.end then leave
  122.         end
  123.  
  124.         call close( 'out' )
  125.  
  126.         address command cmd.k       temp_file
  127.         address command delete_name temp_file
  128.     end
  129. end
  130.  
  131. QuitScript:
  132.  
  133. call pragma( 'D', old_dir )
  134.  
  135. if names.0 > 0 then do
  136.    string = 'Check the' temp_dir 'directory for the file(s):'
  137.  
  138.    do i = 1 to names.0
  139.       string = string || cr || '   ' || names.i
  140.    end
  141.  
  142.    call EMS_Do_Request( string )
  143. end
  144.  
  145. call EMS_FreeScriptData()
  146. exit 0
  147.  
  148. Usage:
  149.  
  150. say 'Usage: ExtractFile.ems <area name> <msg num>'
  151.  
  152. call EMS_FreeScriptData()
  153. exit 0
  154.  
  155.  
  156. error:
  157. syntax:
  158.  
  159. error_text = EMS_LastError()
  160.  
  161. if error_text = '' then error_text = rc ErrorText( rc )
  162.  
  163. say '| ***BREAK: error at' sigl error_text
  164.  
  165. call EMS_FreeScriptData()
  166. exit rc
  167.